package com.assets.newNHF.util;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * @作者(author)： JQ
 * @创建时间(date)： 2020/1/29 17:23
 **/
public class SpUtils {
    private final static String SP_NAME = "setting";
    private SharedPreferences sharedPreferences = null;
    private static SpUtils spUtils = null;

    public static SpUtils getInstance() {
        if (spUtils == null) {
            synchronized (SpUtils.class) {
                if (spUtils == null) {
                    spUtils = new SpUtils();
                }
            }
        }
        return spUtils;
    }

    public void init(Context context) {
        sharedPreferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
    }

    public SharedPreferences.Editor getEditor() {
        return sharedPreferences.edit();
    }

    public void putString(String key, String value) {
        getEditor().putString(key, value).apply();
    }

    public String getString(String key) {
        return sharedPreferences.getString(key, "");//""为该key为空时的默认返回值
    }

    public int getInt(String key) {
        return sharedPreferences.getInt(key, 26);
    }

    public Long getLong(String key) {
        return sharedPreferences.getLong(key, 0);
    }

    public boolean getBoolean(String key) {
        return sharedPreferences.getBoolean(key, false);
    }


}
